package com.youlu.campus.monitor.controller;

import com.youlu.campus.common.base.QResult;
import com.youlu.campus.monitor.entity.WechatQuestion;
import com.youlu.campus.monitor.entity.WechatQuestionPageQuery;
import com.youlu.campus.monitor.service.WechatMonitorQuestionService;
import com.youlu.campus.monitor.service.WechatQuestionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 微信问题接口层
 *
 * @author su
 * @date 2025/09/02
 */
@Slf4j
@RestController
@RequestMapping("/wechat/question")
public class WechatQuestionController {

    @Resource
    private WechatQuestionService questionService;

    @Resource
    private WechatMonitorQuestionService wechatMonitorQuestionService;


    /**
     * 新增微信问题
     * 请求方式：POST
     * 请求体：任务实体（不含id）
     */
    @PostMapping("/save")
    public QResult<WechatQuestion> save(@RequestBody WechatQuestion question) {
        try {
            question = wechatMonitorQuestionService.save(question);
            return QResult.success(question); // 成功：封装任务实体返回
        } catch (Exception e) {
            log.error("新增微信问题接口异常，任务信息：{}", question, e);
            return QResult.fail(e.getMessage()); // 失败：封装错误信息返回
        }
    }

    /**
     * 根据id删除微信问题
     * 请求方式：DELETE
     * 路径参数：任务id
     */
    @PostMapping("/delete")
    public QResult<Boolean> deleteById(@RequestBody WechatQuestion question) {
        boolean deleteResult = questionService.deleteById(question.getId());
        if (deleteResult) {
            return QResult.success(true); // 成功：返回true
        } else {
            return QResult.fail("删除失败：任务不存在"); // 失败：自定义提示
        }
    }

    /**
     * 根据id查询微信问题详情
     * 请求方式：GET
     * 路径参数：任务id
     */
    @GetMapping("/info")
    public QResult<WechatQuestion> getById(@RequestParam String id) {
        WechatQuestion question = questionService.findById(id);
        if (question != null) {
            return QResult.success(question); // 成功：返回任务详情
        } else {
            return QResult.fail("查询失败：任务不存在"); // 失败：自定义提示
        }
    }

    /**
     * 分页查询微信问题列表
     *
     * @param query
     * @return
     */
    @PostMapping("/find-page")
    public QResult<Page<WechatQuestion>> findByPage(@RequestBody WechatQuestionPageQuery query) {
        if (StringUtils.isBlank(query.getMonitorId())) {
            return QResult.fail("监控id不能为null");
        }
        Page<WechatQuestion> page = questionService.findByPage(query);
        return QResult.success(page);
    }
}
    